package com.shoothzj.example.storage.spring.jdbc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.dao.DataAccessException;

@Slf4j
public class JdbcTemplateService {
    private final JdbcTemplate jdbcTemplate;

    public JdbcTemplateService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public long countTable(String schema, String tableName) {
        String sql = "SELECT COUNT(*) FROM ";
        Long result = jdbcTemplate.queryForObject(sql + schema + "." + tableName, Long.class);
        return result == null ? 0 : result;
    }

    public boolean selectOne() {
        try {
            Integer result = jdbcTemplate.queryForObject("SELECT 1", Integer.class);
            return result != null && result == 1;
        } catch (DataAccessException e) {
            log.error("selectOne error", e);
            return false;
        }
    }
}
